from . import user_blue
from flask import *
from info.utils import validate_request as vr
from info.models import *
from info.utils.encript import *
from sqlalchemy import and_
from datetime import datetime, timedelta
from info.utils.format_hour_minute import format_hour_minute

#管理员登录
@user_blue.route('/user/admin_login',methods=['post','get'])
def admin_login_handler():
    if request.method=='GET':
        return render_template('admin/login.html')
    else:
        username = request.form.get('username')
        password = request.form.get('password')
        admin = Admin.query.filter(and_(Admin.username==username,Admin.password==md5(password))).first()
        if not admin:
            return vr.WRONG_CONTENT
        else:
            session['admin'] = admin
            return redirect(url_for('user.admin_index_handler'))

#管理员注销
@user_blue.route('/user/admin_logout')
def admin_logout_handler():
    session.pop('admin')
    return redirect(url_for('user.admin_login_handler'))

#后台首页
@user_blue.route('/user/admin_index')
def admin_index_handler():
    admin = session.get('admin')
    if not admin:
        return vr.error
    return render_template('admin/index.html',admin=admin)

"""
from datetime import datetime, timedelta
 
NOW = datetime.utcnow()
last_24h_submits_count = []
for h in xrange(1,25):
    count = session.query(Status).filter(Status.submit_time.between(NOW - timedelta(seconds=h*3600-1), NOW - timedelta(hours=h-1))).count()
    last_24h_submits_count.append(count)
"""

#用户统计
@user_blue.route('/user/user_count')
def user_count_handler():
    total = User.query.count()
    datetime_now = datetime.now()
    last_12h_login_count = []
    last_12h_login_time = []
    for h in range(1,13):
        count = User.query.filter(
            User.lastLoginDatetime.between(datetime_now - timedelta(seconds=h * 3600 - 1), datetime_now - timedelta(hours=h - 1))).count()
        last_12h_login_count.append(count)
        last_12h_login_time.append(format_hour_minute(datetime_now.hour-h+1,datetime_now.minute))
    month_increment_count = User.query.filter(User.createDatetime > datetime.strptime('%s-%s'%(datetime_now.year,datetime_now.month),'%Y-%m')).count()
    day_increment_count = User.query.filter(User.createDatetime > datetime_now.date()).count()
    return render_template('admin/user_count.html',
                           total = total,
                           last_12h_login_count = last_12h_login_count[::-1],
                           last_12h_login_time = last_12h_login_time[::-1],
                           month_increment_count = month_increment_count,
                           day_increment_count = day_increment_count
                           )
#用户列表
@user_blue.route('/user/user_list')
def user_list_handler():
    current_page = request.args.get('page')
    if not current_page:
        current_page = 1

    paginate_ = User.query.paginate(int(current_page),9)
    user_s = paginate_.items
    total_page = paginate_.pages
    return render_template('admin/user_list.html',user_s=user_s,
                           current_page = current_page,
                           total_page = total_page
                           )